# !/usr/anaconda3/bin python
# -*-coding:utf-8-*-
"""
@Project        :   project_manage
@FileName       :   views.py
@Author         :   tangchaolizi
@Datetime       :   2020-06-16 13:04
@Software       :   PyCharm
@Show           :   项目主页 Views
"""
from django.shortcuts import render, HttpResponse
import datetime
from django_redis import get_redis_connection
from accounts.models import UserRole
from django.utils import timezone
import json
from . import models
# Create your views here.


def index(request):
    return render(request, 'index.html', locals())


def get_menu(request):
    """
    url: /project_page/get_menu/
    获取菜单
    :param request:
    :return: Json-菜单-List
    """
    data = []
    if request.user.is_anonymous:
        # 从数据库内获取未登录用户所能访问的菜单
        menu = models.PageMenu.objects.get(id=3)  # 顶级菜单
        menu_p = models.PageMenu.objects.get(id=10)  # 二级菜单
        # 顶级菜单
        menu_data = {'id': menu.id,
                     'title': menu.title,
                     'icon': menu.icon,
                     'type': int(menu.type),
                     'href': menu.href,
                     'spread': menu.spread}
        # 顶级菜单加入二级菜单
        menu_p_data = [{'id': menu_p.id,
                           'title': menu_p.title,
                           'icon': menu_p.icon,
                           'type': int(menu_p.type),
                           'openType': menu_p.opentype,
                           'href': menu_p.href,
                           'spread': menu_p.spread}]
        menu_data['children'] = menu_p_data
        data.append(menu_data)
        return HttpResponse(json.dumps(data), content_type='application/json')
    else:
        # 从数据库内获取当前用户角色所能访问的菜单
        rid = UserRole.objects.get(userid_id=request.user.id).roleid_id  # 获取用户角色
        menus = models.RoleMenu.objects.filter(role_id=rid).all()  # 获取角色所属菜单
        for menu in menus:
            menu_pid = menu.page_menu.pid
            if menu_pid == '0':  # menu_pid为0则为顶级菜单
                menu_data = {'id': menu.page_menu.id,
                             'title': menu.page_menu.title,
                             'icon': menu.page_menu.icon,
                             'type': int(menu.page_menu.type),
                             'href': menu.page_menu.href,
                             'spread': menu.page_menu.spread}
                menus_p = models.RoleMenu.objects.filter(role_id=rid).all()  # 获取角色所属菜单
                meun_p_data = []
                for menu_p in menus_p:
                    menu_p_type = menu_p.page_menu.type  # 获取角色二级菜单
                    if menu_p_type == '1'  and str(menu_p.page_menu.pid) == str(menu.page_menu.id):
                        menu_p_data = {'id': menu_p.page_menu.id,
                                       'title': menu_p.page_menu.title,
                                       'icon': menu_p.page_menu.icon,
                                       'type': int(menu_p.page_menu.type),
                                       'openType': menu_p.page_menu.opentype,
                                       'href': menu_p.page_menu.href,
                                       'spread': menu_p.page_menu.spread}
                        meun_p_data.append(menu_p_data)
                    menu_data['children'] = meun_p_data
                data.append(menu_data)
        return HttpResponse(json.dumps(data), content_type='application/json')
